﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinPlusCommon
{
    public class FinPlusFuncStack
    {
        public IFinPlusFunction Last 
        { 
            get
            {
                if (_last.Count < 1) 
                    return null;
                var func = _last.Last();
                _last.RemoveLast();
                _next.AddLast(func);
                return func; 
            }
            
            set
            {
                if (!_last.Contains(value))
                {
                    _last.AddLast(value);
                    _next.Clear();
                    if (_last.Count() > _size) 
                        _last.RemoveFirst();
                }
            }
        
        }

        public IFinPlusFunction Next
        {
            get
            {
                if (_next.Count < 1) 
                    return null;
                var func = _next.Last();
                _next.RemoveLast();
                _last.AddLast(func);
                 return func;
            }
        }

        private LinkedList<IFinPlusFunction> _last, _next;
        private int _size;

        public FinPlusFuncStack(int size = 20)
        {
            _size = size;
            _last = new LinkedList<IFinPlusFunction>();
            _next = new LinkedList<IFinPlusFunction>();
        }
    }
}
